<?php

class ErrorController extends Zend_Controller_Action
{
    protected $_errors,$_auth,$_req;

    public function preDispatch(){
        $this->_errors = $this->_getParam('error_handler');
        $this->_auth = Zend_Auth::getInstance()->getIdentity();

    }
    public function errorAction()
    {

    $this->_req = $this->_errors['request'];

        if($this->_req->getParam('module') == 'admin'  && $this->_auth->status === 'admin' || $this->_auth->status === 'demo_admin'){
            $this->getHelper('layout')->setLayout('admin');
            $this->view->status = 'admin';

        }else{
            $this->getHelper('layout')->setLayout('master');
        }

        if (!$this->_errors || !$this->_errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }
        
        switch ($this->_errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Страница не найдена';
				$this->view->error=$this->_errors;
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
				$this->view->error=$this->_errors;
                break;
        }
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->log($this->view->message, $priority, $this->_errors->exception);
            $log->log('Request Parameters', $priority, $this->_errors->request->getParams());
        }
        
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $this->_errors->exception;
        }
        
        $this->view->request = $this->_errors->request;

		$logger=new Zend_Log();
		$config=$this->getInvokeArg('bootstrap')->getOption('logs');
		$txtWriter=new Zend_Log_Writer_Stream($config['logPath']. '/error.log');
		$logger->addWriter($txtWriter);
		$format='Дата ошибки: %timestamp%|Тип ошибки: %priorityName%|Url: %request%|Хост: %host%|Сообщение: %message%|'.PHP_EOL;
		$formatter=new Zend_Log_Formatter_Simple($format);
		$txtWriter->setFormatter($formatter);

		$logger->setEventItem('request',$this->getRequest()->getRequestUri());
		$logger->setEventItem('host',$this->getRequest()->getClientIp());
		$logger->log('Error',Zend_Log::EMERG);
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }
	function accessdeniedAction()
	{

	        $errors = $this->_getParam('error_handler');
        
        if (!$errors || !$errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }
        
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Страница не найдена';
				$this->view->error=$errors;
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
				$this->view->error=$errors;
                break;
        }
        
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->log($this->view->message, $priority, $errors->exception);
            $log->log('Request Parameters', $priority, $errors->request->getParams());
        }
        
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
        
        $this->view->request = $errors->request;
	    $this->view->error=$errors;
	}
}

